package com.wyp168.juc.future;

import com.wyp168.juc.util.ExecutorsUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName CompletableFutureMethodDemo001
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/12 18:20
 */
public class CompletableFutureMethodDemo001 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPool = ExecutorsUtil.getThreadPoolExecutor();
        CompletableFuture.supplyAsync(() -> {
            try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) {e.printStackTrace();}
            int a = 1;
            return a;
        }, threadPool)
                .handle((v, e) -> {
                    System.out.println("abc");
                    int i = 10/0;
                    return v + 2;
                })
                .handle((v ,e) -> {
                    System.out.println("def");
                    return v + 3;
                })
                .whenComplete((v, e) -> {
                    if (e == null) {
                        System.out.println(v);
                    }
                })
                .exceptionally(e -> {
                    System.out.println(e.getMessage() + "\t" + e.getCause());
                    return null;
                });

        //关闭线程池资源
        threadPool.shutdown();

        System.out.println("Main Thread over!");
    }
}
